package business

import (
	"github.com/gin-gonic/gin"
	"gitee.com/dayu1985/can_api/service/can"
	"gitee.com/dayu1985/can_api/controller/base"
)

func Can(context *base.RouteContext) {
	r := context.Privilege.App
	canService := can.NewCanService()

	/**
	 * @api {post} /app/v1.0/can/realtime.json 查询实时数据
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询实时数据
	 *
	 * @apiParam {Array} midwayno  	*必选 集中器编号
	 * @apiParam {Array} stationno 	*可选 站点编号，只有当集中器编号个数为1时，此参数才启用
     * @apiParamExample {json} 请求示例
		{
	       "midwayno":["2A0040000C51524D39383320"],
	       "stationno":["1"]
	    }
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {int} stations.time 时间
	 * @apiSuccess {int} stations.status 状态 (运行 1E=30， 满停 1D=29， 耳停 1B=27， 违停 17=23，经停 0F=16， 其他 1F=31，
	                                补包运行 100，补包停止 101)
	 * @apiSuccess {int} stations.value 转速1
	 * @apiSuccess {int} stations.value1 转速2
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "1",
							"time": 1528515853,
							"status": 30,
							"value": 546,
							"value1": 0
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/realtime.json", func(c *gin.Context) {

		param := can.RealTimeParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.RealTime(&param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/realtime2.json 查询实时数据(新)
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询实时数据(新)
	 *
	 * @apiParam {Array} midways      *必选 集中器集合
	 * @apiParam {String} midwayno    *必选 集中器编号
	 * @apiParam {Array} times 	      *必选 站点编号，只有当集中器编号个数为1时，此参数才启用
	 * @apiParam {int} strattime  	  *必选 开始时间
	 * @apiParam {Array} stationno    *必选 机器编码集合
 	 * @apiParamExample {json} 请求示例
		{
		  "midways":	[
				{
					"midwayno": "510028000851525437383920",
					"times": [
						{
							"strattime": 1533176638,
							"stationno": [
								"18","19","20"
							]
						}
					]
				}
			]
		}
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {int} stations.time 时间
	 * @apiSuccess {int} stations.status 状态 (运行 1E=30， 满停 1D=29， 耳停 1B=27，  违停 17=23，经停 0F=16， 其他 1F=31，
									补包运行 100，补包停止 101 未知运行 200，未知停止 201)
	 * @apiSuccess {int} stations.value 转速1
	 * @apiSuccess {int} stations.value1 转速2
	 * @apiSuccess {float} stations.efficiency 效率
	 * @apiSuccess {int} stations.lasttime 持续时间
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "1",
							"time": 1528515853,
							"status": 30,
							"value": 546,
							"value1": 0,
							"efficiency":0.99,
							"lasttime":100000
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/realtime2.json", func(c *gin.Context) {

		param := can.RealTime2Param{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.RealTime2(param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/switch.json 查询变化状态
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询历史状态
	 *
	 * @apiParam {Array} midwayno  	*必选 集中器编号
	 * @apiParam {Array} stationno 	*可选 站点编号，只有当集中器编号个数为1时，此参数才启用
	 * @apiParam {Array} status 	*可选 状态
	 * @apiParam {int} starttime 	*可选 开始时间
	 * @apiParam {int} endtime 	*可选 截止时间
	 * @apiParam {int} minvalue 	*可选 最小值
	 * @apiParam {int} maxvalue 	*可选 最大值
     * @apiParamExample {json} 请求示例
		{
	       "midwayno":["2A0040000C51524D39383320"],
	       "stationno":["3"],
	       "status":[],
	       "starttime":1528517754,
	       "endtime":0,
	       "minvalue":0,
	       "maxvalue":0
	    }
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {int} stations.time 时间
	 * @apiSuccess {int} stations.status 状态 (运行 1E=30， 满停 1D=29， 耳停 1B=27，  违停 17=23， 经停 0F=16， 其他 1F=31)
	 * @apiSuccess {int} stations.value 转速1
	 * @apiSuccess {int} stations.value1 转速2
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "3",
							"time": 1528517754,
							"status": 30,
							"value": 448,
							"value1": 0
						},
						{
							"stationno": "3",
							"time": 1528522873,
							"status": 30,
							"value": 447,
							"value1": 0
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/switch.json", func(c *gin.Context) {

		param := can.HisParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.SwitchReport(&param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/his.json 查询完整历史报文（最多7天）
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询完整历史数据（最多7天）
	 *
	 * @apiParam {Array} midwayno  	*必选 集中器编号
	 * @apiParam {Array} stationno 	*可选 站点编号，只有当集中器编号个数为1时，此参数才启用
	 * @apiParam {Array} status 	*可选 状态
	 * @apiParam {int} starttime 	*可选 开始时间
	 * @apiParam {int} endtime 	*可选 截止时间
	 * @apiParam {int} minvalue 	*可选 最小值
	 * @apiParam {int} maxvalue 	*可选 最大值
     * @apiParamExample {json} 请求示例
		{
	       "midwayno":["2A0040000C51524D39383320"],
	       "stationno":["3"],
	       "status":[],
	       "starttime":1528517754,
	       "endtime":0,
	       "minvalue":0,
	       "maxvalue":0
	    }
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {int} stations.time 时间
	 * @apiSuccess {int} stations.status 状态 (运行 1E=30， 满停 1D=29， 耳停 1B=27，  违停 17=23，经停 0F=16，  其他 1F=31)
	 * @apiSuccess {int} stations.value 转速1
	 * @apiSuccess {int} stations.value1 转速2
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "3",
							"time": 1528517754,
							"status": 30,
							"value": 448,
							"value1": 0
						},
						{
							"stationno": "3",
							"time": 1528522873,
							"status": 30,
							"value": 447,
							"value1": 0
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/his.json", func(c *gin.Context) {

		param := can.HisParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.HisReport(&param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/packets.json 查询报文（最多7天）
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询报文（最多7天）
	 *
	 * @apiParam {Array} midwayno  	*必选 集中器编号
	 * @apiParam {int} starttime 	*可选 开始时间
	 * @apiParam {int} endtime 	*可选 截止时间
     * @apiParamExample {json} 请求示例
		{
	       "midwayno":["2A0040000C51524D39383320"],
	       "starttime":1528517754,
	       "endtime":0
	    }
	 * @apiSuccess {Array} .报文集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {String} value 原始报文
	 * @apiSuccess {int} time 时间
	 * @apiSuccess {String} source 客户端地址端口
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				  {
						"value": "4C 4C 00 1E 2A 00 40 00 0C 51 52 4D 39 38 33 20 01 00 02 23 1E C1 02 00 01 CC 1E FA 03 00 01 C0 1E A4 9C 4E 4E ",
						"midwayno": "2A0040000C51524D39383320",
						"time": 1528628179,
						"source": "/115.239.67.110:8765"
      			 }
			]
		}
	 *
	 */
	r.POST("/v1.0/can/packets.json", func(c *gin.Context) {

		param := can.PacketParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.PacketReport(&param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/status.json 状态查询（无效）
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 状态查询（无效）
	 *
	 * @apiParam {Array} midways      *必选 集中器集合
	 * @apiParam {String} midwayno    *必选 集中器编号
	 * @apiParam {Array} times 	      *必选 站点编号，只有当集中器编号个数为1时，此参数才启用
	 * @apiParam {int} strattime  	  *必选 开始时间
	 * @apiParam {Array} stationno    *必选 机器编码集合
 	 * @apiParamExample {json} 请求示例
		{
		  "midways":	[
				{
					"midwayno": "510028000851525437383920",
					"times": [
						{
							"strattime": 1533176638,
							"stationno": [
								"18","19","20"
							]
						}
					]
				}
			]
		}
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {int} stations.time 时间
	 * @apiSuccess {int} stations.status 状态 (运行 1E=30， 满停 1D=29， 耳停 1B=27，  违停 17=23，经停 0F=16， 其他 1F=31，
									补包运行 100，补包停止 101)
	 * @apiSuccess {int} stations.value 转速1
	 * @apiSuccess {int} stations.value1 转速2
	 * @apiSuccess {float} stations.efficiency 效率
	 * @apiSuccess {int} stations.lasttime 持续时间
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "1",
							"time": 1528515853,
							"status": 30,
							"value": 546,
							"value1": 0,
							"efficiency":0.99,
							"lasttime":100000
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/status.json", func(c *gin.Context) {

		param := can.StatusParam{}
		base.Action(c, param, func() (interface{}, error) {
			midwys, err := canService.StatusReport(&param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /v1.0/can/hisreport.json 综合查询
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 综合查询
	 *
	 * @apiParam {Array} midways      *必选 集中器集合
	 * @apiParam {String} midwayno    *必选 集中器编号
	 * @apiParam {Array} times 	      *必选 时间分段
	 * @apiParam {int} strattime 	      *必选 开始时间
	 * @apiParam {int} endtime  	  *必选 截止时间
	 * @apiParam {Array} timeparts    *必选 时间段索引（0-47）
	 * @apiParam {int} timeparts.strat    *必选 时间段索引开始（0-47）
	 * @apiParam {int} timeparts.end    *必选 时间段索引截止（0-47）
	 * @apiParam {Array} stationno    *必选 机器编码集合
 	 * @apiParamExample {json} 请求示例
		{
		  "midways":	[
				{
					"midwayno": "510028000851525437383920",
					"times": [
						{
							"strattime": 1533139200,
							"endtime": 1533312100,
	                        "timeparts":[
	                         {
	                            "strat":0,
	                            "end":16
	                         },
	                         {
	                            "strat":17,
	                            "end":32
	                         },
	                         {
	                            "strat":33,
	                            "end":47
	                         }],
							"stationno": [
								"18","19","20"
							]
						}
					]
				}
			]
		}
	 * @apiSuccess {Array} .集中器集合
	 * @apiSuccess {String} midwayno 集中器编码
	 * @apiSuccess {Array} stations 站点集合
	 * @apiSuccess {String} stations.stationno 站点编号
	 * @apiSuccess {Array} stations.efficencys 效率
	 * @apiSuccess {int} stations.efficencys.starttime 开始时间
	 * @apiSuccess {int} stations.efficencys.endtime 截止时间
	 * @apiSuccess {int} stations.efficencys.stoptimes23
	 * @apiSuccess {int} stations.efficencys.stoptimes27
	 * @apiSuccess {int} stations.efficencys.stoptimes29
	 * @apiSuccess {int} stations.efficencys.stoptimes31
	 * @apiSuccess {int} stations.efficencys.stoptimes101
	 * @apiSuccess {int} stations.efficencys.stoptimes201
	 * @apiSuccess {int} stations.efficencys.efficiency 运行时长（秒）
	 * @apiSuccess {int} stations.efficencys.stoptime  停止时长（秒）
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midwayno": "2A0040000C51524D39383320",
					"stations": [
						{
							"stationno": "1",
							"efficencys": [
	                           {
									"starttime": 1533139200,
									"endtime": 1533169800,
									"stoptimes23": 11,
									"stoptimes27": 0,
									"stoptimes29": 0,
									"stoptimes31": 2,
									"stoptimes101": 0,
									"stoptimes201": 0,
									"efficiency": 27676,
									"stoptime": 2924
	                           }
	                        ]
						}
					]
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/hisreport.json", func(c *gin.Context) {
		param := can.HisReposrtParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.HisEfficiencyReport(param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

	/**
	 * @api {post} /app/v1.0/can/realtimeinfo.json 查询当前状态
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询当前状态
	 *
     * @apiParamExample {json} 请求示例
		{
	    }
	 * @apiSuccess {Array} midways 站点集合
	 * @apiSuccess {String} midways.midwayno 中层编号
	 * @apiSuccess {Array}  midways.stations 中层站点集合
	 * @apiSuccess {int} midways.stationsize 中层站点数量
	 * @apiSuccess {int} midwaysize 中层数量
	 * @apiSuccess {int} stationsize 站点数量
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"midways": [
						{
							"midwayno": "350040000C51524D39383320",
							"stations": [
								"49"
							],
							"stationsize": 1
						}
	                 ],
	                "midwaysize": 36,
                    "stationsize": 2791
				}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/realtimeinfo.json", func(c *gin.Context) {

		base.Action(c, nil, func() (interface{}, error) {
			midwys, err := canService.RealTimeInfo()
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})

	})

	/**
	 * @api {post} /app/v1.0/can/packinfo.json 查询报文上报次数
	 * @apiGroup can
	 * @apiVersion 1.0.0
	 * @apiDescription 查询报文上报次数
	 *
	 * @apiParam {String} midwayno    *必选 集中器编号
	 * @apiParam {int} strattime 	  *必选 开始时间
	 * @apiParam {int} endtime  	  *必选 截止时间
     * @apiParamExample {json} 请求示例
		{
	       "midwayno":"420042000751525437383920",
	       "starttime":1551718246,
	       "endtime":1551718726
	     }
	 * @apiSuccess {String} stationno 站点编号
	 * @apiSuccess {int} packsize 上报次数
	 * @apiSuccessExample HTTP/1.1 200 OK:
		{
			"Code": 0,
			"Message": "ok",
			"Response": [
				{
					"stationno": "3",
					"packsize": 25
				},
				{
					"stationno": "12",
					"packsize": 26
        		}
			]
		}
	 *
	 */
	r.POST("/v1.0/can/packinfo.json", func(c *gin.Context) {
		param := can.PacketInfoParam{}
		base.Action(c, &param, func() (interface{}, error) {
			midwys, err := canService.PacketInfoReport(param)
			if err != nil {
				return nil, err
			}
			return midwys, nil

		})
	})

}

func init() {
	base.Register(Can)
}
